package com.tms.app.tms.ui.taskinfo;

import android.content.Intent;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tms.app.tms.adapter.NodeAdapter;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.bean.Node;
import com.tms.app.tms.bean.TestBean;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.TaskInfoRes;
import com.tms.app.tms.ui.base.presenter.AbsServicexUtils;
import com.tms.app.tms.ui.base.view.IListView;
import com.tms.app.tms.ui.base.presenter.AbsBaseListP;
import com.tms.app.tms.ui.NodeTaskAC;
import com.tms.app.tms.utils.ImageUtil;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import cn.edu.zafu.camera.BdJson;
import cn.edu.zafu.camera.OrcText;
import cn.edu.zafu.camera.OrcTextUtil;

/**
 * Created by Administrator on 2017/10/28 0028.
 */

public class TaskInfoP extends AbsBaseListP {
   // 派车单状态 0 待执行，1执行中，2 司机已完成3，确认已完成
    private int planId;
    private int doType=0;
    private int planIdShowType=-1;

    public void setPlanIdShowType(int planIdShowType) {
        this.planIdShowType = planIdShowType;
    }

    @Override
    public void setiView(IListView iViewTrade) {
        super.setiView(iViewTrade);
    }


    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        if (requestType==1){// 任务详情
            TaskInfoRes taskInfoRes=gson.fromJson(result,TaskInfoRes.class);
            bindDataToView(taskInfoRes);
            sendMsgToView(taskInfoRes);
            return;
        }
        if (requestType==2){// 拒绝/接收
            BaseResponse baseResponse=gson.fromJson(result,BaseResponse.class);
            baseResponse.setWorkType(doType);
            sendMsgToView(baseResponse);
        }
    }





    private void bindDataToView(TaskInfoRes taskInfoRes){
        if (iView==null)
            return;
        if (taskInfoRes.getCode()!=200||taskInfoRes.getData()==null
                ||taskInfoRes.getData().getStationExecList()==null
                ||taskInfoRes.getData().getStationExecList().size()<1){
            iView.refreshNoData();
            return;
        }
        NodeAdapter depthAdapter= iView.getAdapter(0);
        depthAdapter.getData().clear();
        depthAdapter.getData().addAll(taskInfoRes.getData().getStationExecList());
        Node node=new Node();
        node.setStatus(taskInfoRes.getData().getStationExecList().get(taskInfoRes.getData().getStationExecList().size()-1).getStatus());
        depthAdapter.getData().add(node);
        depthAdapter.notifyDataSetChanged();
        iView.refreshComplite("");
    }

    private void setTaskStatus(int doType){
        if (iView==null)
            return;
        NodeAdapter depthAdapter= iView.getAdapter(0);
        List<Node> list =depthAdapter.getData();
        depthAdapter.notifyDataSetChanged();
        sendMsgToView(doType);
    }


    /**
     * 任务详情
     * @param token
     * @param planId
     */
    public void getData(String token,int planId){
        if (planId==-1)
            return;
        this.token=token;
        this.planId=planId;
        this.doType=0;
        requestType=1;
        RequestParams params=new RequestParams(Constant.Task_detail);
        params.addBodyParameter("token",token);
        params.addBodyParameter("planId",planId+"");
        params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
        getObjectFromServer(params,1);

    }


    /***
     * 执行或拒绝
     * @param token
     * @param planId
     * @param doType
     */
    public void doRefuseTask(String token,int planId,int doType){
        if (planId==-1)
            return;
        this.doType=doType;
        RequestParams params=new RequestParams(Constant.DO_Refus_Task);
        params.addBodyParameter("token",token);
        params.addBodyParameter("planId",planId+"");
        params.addBodyParameter("doType",doType+"");
        params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
        getObjectFromServer(params,1);
        requestType=2;
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        getData(token,planId);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Node node= (Node) adapter.getData().get(position);
        if (position==0||position==adapter.getData().size()-1)
            return;
        if (node.getPlanIdShowType()==0||node.getPlanIdShowType()==10)//派车单状态 0 待执行，1执行中，2 司机已完成3，确认已完成4 挂起
        {
            return;
        }

        if (node.getStatus()==0)//
        {
            return;
        }

        if (true){

            Intent intent=new Intent(view.getContext(), NodeTaskAC.class);
            intent.putExtra("stationExecId",node.getStationExecId());
            intent.putExtra("planIdShowType",planIdShowType);
            intent.putExtra("doType", node.getDoType());
            iView.startActivityForResult(intent,1);
        }
    }
}
